﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using ArlesInterfaces.Editor;
using Arles.src.controller;

namespace Arles.src.view.controls
{
    public class TrackBarItem : UITypeEditor
    {
        Object actualItem;
        IServiceProvider actualProvider;
        TrackBarProperty properties;


        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            actualItem = context.Instance;
            actualProvider = provider;   

            IWindowsFormsEditorService wfes = provider.GetService(

                typeof(IWindowsFormsEditorService)) as

                IWindowsFormsEditorService;

            if (wfes != null)
            {
                string s = actualProvider.GetType().GetProperty("PropertyName").GetValue(actualProvider, null).ToString();
                properties = ((ITrackBarProperties)context.Instance).TrackBarProperties[s];

                TackBarControl tb = new TackBarControl();
                tb.ValueChanged += new TackBarControl.TrackBarArgs(tb_ValueChanged);
                tb.Minimum = properties.Min;
                tb.Maximum = properties.Max;
                tb.Div = properties.Div;
                tb.Value = (int)((float)value * (int)properties.Div);
                tb.TickFrequency = properties.TickFrequency;


                wfes.DropDownControl(tb);

                value = tb.Value / properties.Div;
            }

            return value;
        }

        void tb_ValueChanged(int value)
        {
            string s = actualProvider.GetType().GetProperty("PropertyName").GetValue(actualProvider, null).ToString();
            actualItem.GetType().GetProperty(s).SetValue(actualItem, value / properties.Div, null);
        }
    }
}
